package com.hnevc.sqlitedemo;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.util.Log;

public class MyContentProvider extends ContentProvider {
    public static final String TAG="MyContentProvider";
    MySQLiteOpenHelper mOpenHelper;
    public MyContentProvider() {

    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        Log.e(TAG, "delete: " );
        return db.delete("news",selection,selectionArgs);
    }

    @Override
    public String getType(Uri uri) {
        Log.e(TAG, "getType: " );
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        Uri returnUri=null;
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        long id = db.insert("news",null,values);
        returnUri = new Uri.Builder()
                .scheme("content")
                .authority("com.hnevc.sqlitedemo")
                .path("news")
                .appendPath(id+"")
                .build();
        Log.e(TAG, "insert: "+returnUri.toString() );
        return returnUri;
    }

    @Override
    public boolean onCreate() {
        mOpenHelper = new MySQLiteOpenHelper(getContext(),"news.db",null,1);
        Log.e(TAG, "onCreate: ");
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        Log.e(TAG, "query: " );
        return db.query("news",projection,selection,selectionArgs,null,null,sortOrder);
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        Log.e(TAG, "update: " );
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        return db.update("news",values,selection,selectionArgs);
    }
}
